// components/zts-infinite-scroll/zts-infinite-scroll.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        requestUrl: {
            type: String,
            value: '',
        },
        params: {
            type: Object,
            value: () => {},
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        pageNum: 0,
        loading: true,
        loadingAll: false,
        // 分页加载返回值数组
        responseArr: [],
    },

    lifetimes: {
        attached: function () {
            // 在组件实例进入页面节点树时执行
            this.getMoreList();
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        getMoreList() {
            let _that = this;
            if (!_that.data.loadingAll) {
                _that.setData({
                    pageNum: _that.data.pageNum + 1,
                    loading: true,
                    loadingAll: false,
                });
                wx.request({
                    url: app.globalData.rootPath + _that.data.requestUrl,
                    data: Object.assign(
                        {
                            authId: app.globalData.authId,
                            pageSize: 20,
                            pageNo: _that.data.pageNum,
                        },
                        _that.data.params
                    ),
                    success(res) {
                        if (res.data.status === '0') {
                            _that.data.responseArr.push(res.data);
                            _that.setData(
                                {
                                    responseArr: _that.data.responseArr,
                                    loading: false,
                                },
                                () => {
                                    _that.triggerEvent('load');
                                }
                            );

                            if (
                                res.data.pager.currentPage >=
                                res.data.pager.totalPages
                            ) {
                                _that.setData(
                                    {
                                        loadingAll: true,
                                    },
                                    () => {
                                        _that.triggerEvent('loadingOver');
                                    }
                                );
                            }
                        } else {
                            _that.setData(
                                {
                                    loadingAll: true,
                                    loading: false,
                                },
                                () => {
                                    _that.triggerEvent('loadingOver');
                                }
                            );
                        }
                    },
                });
            }
        },

        refresh() {
            this.setData(
                {
                    pageNum: 0,
                    loading: true,
                    loadingAll: false,
                    responseArr: [],
                },
                () => {
                    this.getMoreList();
                }
            );
        },
    },
});
